var userApi = require('./api/userApi')
var shopApi = require('./api/shopApi')
var goodsApi = require('./api/goodsApi')
var orderApi = require('./api/orderApi')
var shopAddrApi = require('./api/shopAddrApi')



var multipart = require('connect-multiparty')
var express = require('express')
var bodyParser = require('body-parser')
var path = require("path")

var authTokenMiddleware = require('./middleware/authToken')
var multipartMiddleware = multipart()

var routers = function (app) {
    app.all('*',function (req, res, next) {
        res.header("Access-Control-Allow-Origin", "*");
        next();
    });

    app.use("/public", express.static(path.join(__dirname, 'public')))
    app.use(multipart({ uploadDir: './public/img' }))
    

    app.use(bodyParser.json())
    app.use(bodyParser.urlencoded({ extended: true }))

    //user
    app.post("/api/v1/login", userApi.login)
    app.post("/api/v1/register", multipartMiddleware, userApi.register)
    app.get("/api/v1/users", userApi.listUser)
    app.get("/api/v1/me",authTokenMiddleware,userApi.me)
    app.post("/api/v1/updateUser",authTokenMiddleware,multipartMiddleware,userApi.updateUser)
    //shop
    app.post("/api/v1/createShop", authTokenMiddleware, multipartMiddleware, shopApi.createShop)
    app.post("/api/v1/updateShop", authTokenMiddleware, multipartMiddleware, shopApi.updateShop)
    app.get("/api/v1/shops", shopApi.listShopByAddr)
    app.get("/api/v1/myShop", authTokenMiddleware, shopApi.myShop)
    app.post("/api/v1/publicNotice", shopApi.publicNotice)
    app.get("/api/v1/openShop", authTokenMiddleware, shopApi.openShop)
    app.get("/api/v1/closeShop", authTokenMiddleware, shopApi.closeShop)
    
    //goods
    app.post("/api/v1/createGoods", authTokenMiddleware, multipartMiddleware, goodsApi.createGoods)
    app.get("/api/v1/goodsByType", goodsApi.listGoodsByType)
    app.get("/api/v1/goodsById", goodsApi.listGoodsById)
    app.get("/api/v1/goods", goodsApi.listGoods)
    app.get("/api/v1/offGoods", authTokenMiddleware, goodsApi.offGoods)
    app.get("/api/v1/onGoods", authTokenMiddleware, goodsApi.onGoods)
    app.post("/api/v1/updateGoods", authTokenMiddleware, multipartMiddleware, goodsApi.updateGoods)
    //like
    app.post("/api/v1/like", authTokenMiddleware, goodsApi.like)
    app.post("/api/v1/unlike", authTokenMiddleware, goodsApi.unlike)
    //order
    app.post("/api/v1/createOrder", authTokenMiddleware, orderApi.createOrder)
    app.post("/api/v1/finishOrder", authTokenMiddleware, orderApi.finishOrder)
    app.post("/api/v1/failOrder", authTokenMiddleware, orderApi.failOrder)
    app.post("/api/v1/receiveOrder", authTokenMiddleware, orderApi.receiveOrder)
    app.get("/api/v1/listOrderByStatus", authTokenMiddleware, orderApi.listOrderByStatus)
    app.get("/api/v1/listShopOrder", authTokenMiddleware, orderApi.listShopOrder)
    app.get("/api/v1/admin/listOrderByStatus", orderApi.listShopOrderAdmin)
    app.get("/api/v1/admin/listAllOrder",orderApi.listAllOrder)
    app.get("/api/v1/getToday",authTokenMiddleware,orderApi.getTodayOrder)
    app.get("/api/v1/getMonth",authTokenMiddleware,orderApi.getMonthOrder)
    app.get("/api/v1/getMonthChartOrder",authTokenMiddleware,orderApi.getMonthChartOrder)
    app.get("/api/v1/getYearChartOrder",authTokenMiddleware,orderApi.getYearChartOrder)

    
    //shopAddr
    app.post("/api/v1/shopaddr/create", authTokenMiddleware, shopAddrApi.create)
    app.post("/api/v1/shopaddr/delete", authTokenMiddleware, shopAddrApi.delete)
    app.post("/api/v1/shopaddr/update", authTokenMiddleware, shopAddrApi.update)
    app.get("/api/v1/shopaddr/list", authTokenMiddleware, shopAddrApi.list)
    app.post("/api/v1/shopaddr/setDefault", authTokenMiddleware, shopAddrApi.setDefalt)
    app.get("/api/v1/shopaddr/getDefault", authTokenMiddleware, shopAddrApi.getDefaultAddr)

}

module.exports = routers